﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Radius.NET.Interface.Kingdee.Entity
{
    /// <summary>
    /// 收料通知单
    /// </summary>
    public class PUR_ReceiveBill: KingdeePubClass
    {
        public PUR_ReceiveBill_Model Model { set; get; }

        #region 收料通知单表头
        /// <summary>
        /// 收料通知单表头
        /// </summary>
        public class PUR_ReceiveBill_Model
        {
            /// <summary>
            /// 
            /// </summary>
            public int FID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FBillTypeID FBillTypeID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FBusinessType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public DateTime? FDate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockOrgId FStockOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FDemandOrgId FDemandOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FPurOrgId FPurOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSUPPLIERID FSupplierId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FNote { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSupplyId FSupplyId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSettleId FSettleId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FChargeId FChargeId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FOwnerTypeIdHead { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FOwnerIdHead FOwnerIdHead { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FIsInsideBill { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FIsMobile { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FIsChangeQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FACCTYPE { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<FDetailEntityItem> FDetailEntity { get; set; }
        }
        #endregion

        #region 采购订单明细
        public class FDetailEntityItem
        {
            /// <summary>
            /// 
            /// </summary>
            public int FEntryID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FSEQ { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FMaterialId FMaterialId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FMaterialDesc { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FUnitID FUnitId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public DateTime? FPreDeliveryDate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FSUPDELQTY { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FPriceUnitID FPriceUnitId { get; set; }

            public double? FEntryTaxAmount { get; set; }
            public double? FAllAmount_LC { get; set; }
            public double? FAllAmount { get; set; }
            public double? FTaxAmount_LC { get; set; }
            public double? FAmount { get; set; }
            public double? FAmount_LC { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockId FStockID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockLocId FStockLocId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockStatusId FStockStatusId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FLot FLot { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FGiveAway { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FCtrlStockInPercent { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FCheckInComing { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FIsReceiveUpdateStock { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FDiscountRate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FTaxNetPrice { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FTaxPrice { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FStockInMaxQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FStockInMinQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FEntryTaxRate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FPriceBaseQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockUnitId FStockUnitID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FStockQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FStockBaseQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FActlandQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? F_PYBU_CPGG { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FBucketQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FActReceiveQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public DateTime? F_PYBU_SCRQ { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string F_PYBU_CJPC { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int? FSrcId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FSrcEntryId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSrcFormId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSrcBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FOrderBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FPOORDERENTRYID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FINSTOCKJOINBASEQTY { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<FDetailEntity_LinkItem> FDetailEntity_Link { get; set; }
            public double FPrice { get; internal set; }
        }
        #endregion

        #region 采购订单明细关联表
        public class FDetailEntity_LinkItem
        {
            /// <summary>
            /// 
            /// </summary>
            public string FDetailEntity_Link_FRuleId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FDetailEntity_Link_FSTableName { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FDetailEntity_Link_FSBillId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FDetailEntity_Link_FSId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FDetailEntity_Link_FBaseUnitQtyOld { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FDetailEntity_Link_FBaseUnitQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FDetailEntity_Link_FStockBaseQtyOld { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FDetailEntity_Link_FStockBaseQty { get; set; }
        }
        #endregion

        public class FStockLocId
        {
            public FSTOCKLOCID__FF100002_Model FSTOCKLOCID__FF100002 { get; set; }
            public class FSTOCKLOCID__FF100002_Model
            {
                public string FNumber { get; set; } = string.Empty;
            }
        }

        public object BuildData(InventoryEvidence item, List<PUR_PurchaseOrderDto> pUR_ReceiveBillDto)
        {
            var obj = new PUR_ReceiveBill()
            {
                Model = new()
                {
                    FID = 0,
                    FBillTypeID = new()
                    {
                        FNumber = "SLD01_SYS"
                    },
                    FBusinessType = "CG",
                    FBillNo = item.Evidence_Number,
                    FDate = item.Date,
                    FStockOrgId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FDemandOrgId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FPurOrgId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FSupplierId = new()
                    {
                        FNumber = item.ContactNum
                    },
                    FNote = item.Remark,
                    FSupplyId = new()
                    {
                        FNumber = item.ContactNum
                    },
                    FSettleId = new()
                    {
                        FNumber = item.ContactNum
                    },
                    FChargeId = new()
                    {
                        FNumber = item.ContactNum
                    },
                    FOwnerTypeIdHead = "BD_Supplier",
                    FOwnerIdHead = new()
                    {
                        FNumber = item.ContactNum
                    },
                    FIsInsideBill = false,
                    FIsMobile = false,
                    FIsChangeQty = false,
                    FACCTYPE = "Q",
                    FDetailEntity = new List<FDetailEntityItem>()
                }
            };
            foreach (var e in item.FG_InvDetail)
            {
                var srcItem = pUR_ReceiveBillDto.Find(x => x.FMaterialId_FNumber == e.Inventory_ID);
                if (srcItem is not null)
                {
                    var child = new FDetailEntityItem()
                    {
                        FEntryID = 0,
                        FSEQ = e.NOID,
                        FMaterialId = new()
                        {                //物料编码
                            FNumber = e.Inventory_ID
                        },
                        FMaterialDesc = "",
                        FUnitId = new()
                        {                    //收料单位
                            FNumber = e.FG_Inventory.Unit ?? "GONGJIN01"
                        },
                        FPreDeliveryDate = item.Date,   //预计到货日期
                        FSUPDELQTY = e.Qty,
                        FPriceUnitId = new()
                        {               //计价单位
                            FNumber = e.FG_Inventory.Unit ?? "GONGJIN01"
                        },
                        FStockID = new()
                        {                   //仓库
                            FNumber = item.Stock_ID
                        },
                        FStockLocId = new()
                        {                //仓位
                            FSTOCKLOCID__FF100002 = new()
                            {
                                FNumber = e.Stk_ID
                            }
                        },
                        FStockStatusId = new()
                        {         //库存状态
                            FNumber = "KCZT02_SYS"
                        },
                        FLot = new()
                        {                   //批号
                            FNumber = e.ProjectBatch
                        },
                        //FGiveAway = e.FGiveAway,	//是否赠品
                        FPrice = srcItem.FPrice,   //单价
                        FTaxPrice = srcItem.FTaxPrice,     //含税单价
                        FTaxNetPrice = srcItem.FTaxPrice, //净价
                        FEntryTaxAmount = srcItem.FEntryTaxAmount, //税额
                        FTaxAmount_LC = srcItem.FTaxAmount_LC,   //税额本位币
                        FAllAmount = srcItem.FAllAmount,   //价税合计
                        FAllAmount_LC = srcItem.FAllAmount_LC,   //价税合计本位币  
                        FAmount = srcItem.FAmount,   //金额
                        FAmount_LC = srcItem.FAmount_LC,   //金额本位币
                        FCtrlStockInPercent = true,
                        FCheckInComing = true,
                        FIsReceiveUpdateStock = false,
                        //FDiscountRate = e.Discount,     //折扣率
                        FStockInMaxQty = e.Qty,
                        FStockInMinQty = e.Qty,
                        FEntryTaxRate = srcItem.FEntryTaxRate,          //税率%
                        FPriceBaseQty = e.Qty,
                        FStockUnitID = new()
                        {           //库存单位
                            FNumber = e.FG_Inventory.Unit ?? "GONGJIN01"
                        },
                        FStockQty = e.Qty,
                        FStockBaseQty = e.Qty,
                        FActlandQty = e.Qty,
                        F_PYBU_CPGG = e.Unit_weight,
                        FBucketQty = e.NPKG,     //桶数
                        FActReceiveQty = e.Qty,
                        F_PYBU_SCRQ = e.MakeBeginDate,    //生产日期
                        F_PYBU_CJPC = e.SupBatch,           //厂家批次
                        FSrcId = srcItem.FID,          //源单内码
                        FSrcEntryId = srcItem.FPOOrderEntry_FEntryID,           //源单分录内码
                        FSrcFormId = "PUR_PurchaseOrder",  //源单类型
                        FSrcBillNo = item.OrderID,      //源单单号
                        FOrderBillNo = item.OrderID,         //订单单号
                        FPOORDERENTRYID = srcItem.FPOOrderEntry_FEntryID,
                        FINSTOCKJOINBASEQTY = 0,
                        FDetailEntity_Link = new List<FDetailEntity_LinkItem>() {
                            new (){
                                FDetailEntity_Link_FRuleId= "PUR_PurchaseOrder-PUR_ReceiveBill",
                                FDetailEntity_Link_FSTableName= "t_PUR_POOrderEntry",
                                FDetailEntity_Link_FSBillId= srcItem.FID,
                                FDetailEntity_Link_FSId= srcItem.FPOOrderEntry_FEntryID,
                                FDetailEntity_Link_FBaseUnitQtyOld= e.Qty,
                                FDetailEntity_Link_FBaseUnitQty= e.Qty,
                                FDetailEntity_Link_FStockBaseQtyOld= e.Qty,
                                FDetailEntity_Link_FStockBaseQty= e.Qty
                            }
                        }
                    };
                    obj.Model.FDetailEntity.Add(child);
                }
            }
            return obj;
        }
    }

}
